﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HideOrShowSet : IExternalCommand
	{
		Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Transaction transaction = null;
			ClassHideOrShowOpera classHideOrShowOpera = new ClassHideOrShowOpera(activeUIDocument, document);
			Result result;
			try
			{
				FormHideOrShowSet formHideOrShowSet = new FormHideOrShowSet();
				formHideOrShowSet.ShowDialog();
				if (formHideOrShowSet.DialogResult == DialogResult.Cancel)
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					transaction = new Transaction(document, "隐藏或显示");
					transaction.Start();
					HideOrShow hideOrShow = formHideOrShowSet.hideOrShow;
					if (hideOrShow != HideOrShow.Hide)
					{
						if (hideOrShow == HideOrShow.Show)
						{
							classHideOrShowOpera.ShowOprea(formHideOrShowSet.selectObjs);
						}
					}
					else
					{
						classHideOrShowOpera.HideOprea(formHideOrShowSet.selectObjs);
					}
					transaction.Commit();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
